var $tbody = $("#sample-table-1 tbody");
var allStation = [];
//getAllStation();
var nowdate = new Date();
var oneweekdate = new Date(nowdate-7*24*3600*1000);
$("#startdate").val(beginDate(nowdate));
$("#enddate").val(endDate(oneweekdate));

function beginDate(date) {
    return endDate(date);
};

function endDate(date) {
    return date.getFullYear() + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2);
};
// 初始化table
$("#code_table").bootstrapTable({
    'url': '../user/queryPromotion',
    'cache':false,
    'toolbar': $('div[data-name="btnCtl"]'),
    'toolbarAlign': 'right',
    // 'queryParams': function (params) {
    //     params["orderNo"] = $("#orderno").val();
    //     params["customerName"] = $("#csname").val();
    //     params["customerPhoneNo"] = $("#csphone").val();
    //     params["startTime"] = $("#startdate").val();
    //     params["endTime"] = $("#enddate").val();
    //     return params;
    // },
    'striped': true,
    'pagination': true,
    'sortable': true,
    'sortOrder': "createDate",
    'sidePagination': 'server',
    'pageSize': globalPageSize,
    'pageList': [],
    'clickToSelect': true,
    'singleSelect': true,
    'smartDisplay': true,
    'responseHandler': function (resb) {
        return {
            'total': resb.data.total,
            'rows': resb.data.rows,
        }
    },
    'onCheck': function () {
        $("button[data-target='#updateModal'], button[data-target='#deleteModal']").removeAttr("disabled");
    },
    'onUncheck': function () {
        $("button[data-target='#updateModal'], button[data-target='#deleteModal']").attr("disabled", 'true');
    },
    'onPageChange': function () {
        // console.log($("#order_table").bootstrapTable('getSelections')[0]=='undefined');
        if (typeof($("#order_table").bootstrapTable('getSelections')[0]) != 'undefined') {
            $("#order_table").bootstrapTable('uncheckBy', {
                field: 'orderId',
                values: [$("#order_table").bootstrapTable('getSelections')[0].orderId]
            });
        }
    },
    'columns': [{
        'checkbox': true,
        'class': 'hide'
    }, {
        'field': 'userId',
        'title': 'ID',
        'align': 'center',
        'class': 'hide'

    },
        {
            'field': 'nickName',
            'title': '微信昵称',
            'width':150,
            'align': 'center'

        },{
            'field': 'openid',
            'title': '微信ID',
            'width':100,
            'align': 'center'

        },{
            'field': 'gender',
            'title': '性别',
            'width':100,
            'align': 'center',
            'formatter': function (gender) {
                if (gender == '1') {
                    gender = '男';
                } else if (gender == '2') {
                    gender = '女';
                }
                return gender === null ? '未知' : gender;
            }
        },{
            'field': 'phoneNo',
            'title': '手机号',
            'width':150,
            'align': 'center'
        },{
            'field': 'province',
            'title': '省',
            'width':150,
            'align': 'center'
        },{
            'field': 'city',
            'title': '市',
            'width':150,
            'align': 'center'
        },{
            'field': 'inviteCount',
            'title': '总推荐人数',
            'width':150,
            'align': 'center'
        },{
            'field': 'inviteOrders',
            'title': '推广订单数',
            'width':150,
            'align': 'center'
        },{
            'field': 'inviteTrades',
            'title': '推广交易额(￥)',
            'width':150,
            'align': 'center'
        }]
});

/**
 * 角色查询ajax
 * @param stationName 请求查询的车站名
 */

function orderQuery(nickName) {
    if ( $("#queryterm").val() == '') {
        $('#code_table').bootstrapTable('refreshOptions', {pageNumber: 1, pageSize: globalPageSize});
    } else {
        $.ajax({
            type: "POST",
            url: '../user/queryPromotion',
            data: {
                "offset": 0,
                "limit": 15,
                "nickName": nickName
                //"phoneNo": phoneNo
            },
            dataType: 'json',
            'sidePagination': 'server',
            success: function (resb) {
                //console.log(resb.data.total);
                if (resb.data.length != 0) {
                    $("#code_table").bootstrapTable('load', {
                        'total': resb.data.total,
                        'rows': resb.data.rows,
                    });
                }
                else {
                    $("#code_table").bootstrapTable('removeAll');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }
        });
    }
    return false;
};
/**
 * 查询详情
 *
 */
$(document).on('click','.spanIcons',function(){
	$('#queryterm').val('');
	if($('#queryterm').val() ==""){
		$('.spanIcons').removeClass('glyphicon glyphicon-remove');
	}
});
$("#queryterm").keyup(function(){
	if($('#queryterm').val() !=""){
		$('.spanIcons').addClass('glyphicon glyphicon-remove');
	}
	if($('#queryterm').val() ==""){
		$('.spanIcons').removeClass('glyphicon glyphicon-remove');
	}
})

/**
 * 查询表单提交
 */
/*$("#stationFormId").on('click', function () {
    if(($("#enddate").val() != "")){
     var isdate = new Date($("#enddate").val().replace(/-/g,"/"));
     isdate = new Date((isdate/1000+86400)*1000);
     pdate = isdate.getFullYear()+"-"+(isdate.getMonth()+1)+"-"+(isdate.getDate());
     return orderQuery($("#orderno").val(),$("#csname").val(),$("#csphone").val(),$("#startdate").val(),pdate);
     }else{
     return orderQuery($("#orderno").val(),$("#csname").val(),$("#csphone").val(),$("#startdate").val(),);
     };
    return orderQuery($("#queryterm").val());
});*/
$("#stationFormId").submit(function () {
    return orderQuery($("#queryterm").val());
});
// jQuery(function ($) {
//     $('#id-disable-check').on('click', function () {
//         var inp = $('#form-input-readonly').get(0);
//         if (inp.hasAttribute('disabled')) {
//             inp.setAttribute('readonly', 'true');
//             inp.removeAttribute('disabled');
//             inp.value = "This text field is readonly!";
//         }
//         else {
//             inp.setAttribute('disabled', 'disabled');
//             inp.removeAttribute('readonly');
//             inp.value = "This text field is disabled!";
//         }
//     });
//     $('.date-picker').datepicker({autoclose: true}).next().on(ace.click_event, function () {
//         $(this).prev().focus();
//     });
// });

/**
 * 导出表格
 */
function exportExcel(){
   // var enddate = endDate(oneweekdate);
    var url= '../user/downPromotion';

    $('body').append("<iframe id=\"downloadcsv\" style=\"display:none\"></iframe>");
    $('#downloadcsv').attr('src',url);
};

$("#derive").on('click',function(){exportExcel()});
